package JMSRaceServer;

import javax.jms.*;
import javax.naming.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class JMSReceiverPRUEBA {
	public static void main(String[] args) {
		String queueName = null;
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueReceiver queueReceiver = null;
		TextMessage textMessage = null;
		ObjectMessage objectMessage = null;
		
		if (args.length != 1)
		{
			System.out.println("Usage: java SimpleQueueReceiver <queue-name>");
			System.exit(1);
		}
		queueName = new String(args[0]);
		System.out.println("Queue name is " + queueName);
		try
		{
			Properties props = new Properties();
			System.out.println("properties");
			props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
			System.out.println("setproperty");
			props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
			System.out.println("setproperty2");
			props.load(new FileInputStream("jndi.properties"));
			System.out.println("props.load");
			jndiContext = new InitialContext(props);
			System.out.println("jndiContext = new...");
		}
		catch (NamingException e) 
		{
			System.out.println("Could not create JNDI API context: " + e.toString());
			System.exit(1);
		}
		catch(IOException ioe)
		{
			System.out.println("Could not find properties file: " + ioe.toString());
			System.exit(1);
		}
		try
		{
			queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
			System.out.println("queueConnectionFactory = ...");
			queue = (Queue) jndiContext.lookup(queueName);
			System.out.println("queue = (queue)...");
		}
		catch (NamingException e)
		{
			System.out.println("JNDI API lookup failed: " + e.toString());
			System.exit(1);
		}
		try
		{
			queueConnection = queueConnectionFactory.createQueueConnection();
			System.out.println("queueConnection");
			queueSession = queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
			System.out.println("queueSession");
			if(queueSession==null)
			{
				System.out.println("queueSession==null");
			}
			queueReceiver = queueSession.createReceiver(queue);
			System.out.println("queueReceiver");
			if(queueReceiver ==null)
			{
				System.out.println("queueReceriver==null");
			}
			queueConnection.start();
			System.out.println("queueConnection.start");
			while (true)
			{
				Message m = queueReceiver.receive(1);
				if (m != null)
				{
					if (m instanceof TextMessage)
					{
						textMessage = (TextMessage) m;
						System.out.println("Reading message: " + textMessage.getText());
					}
					else if (m instanceof ObjectMessage)
					{
						objectMessage = (ObjectMessage) m;
						RaceJMSDTO race = (RaceJMSDTO) objectMessage.getObject();
						System.out.println("Reading message: " + race.getName());
					}
					else {break;}
				}
			}
		}
		catch (JMSException e)
		{
			System.out.println("Exception occurred: " + e.toString());
		}
		finally
		{
			if (queueConnection != null) {
				try
				{
					queueConnection.close();
				}
				catch (JMSException e) {}
			}
		}
	}
}
